package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.DetailActivity;
import flc.ast.activity.GuideActivity;
import flc.ast.activity.GuideDetailActivity;
import flc.ast.adapter.GuideTabAdapter;
import flc.ast.databinding.FragmentTacticBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TacticFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TacticFragment extends BaseNoModelFragment<FragmentTacticBinding> {

    private GuideTabAdapter guideTabAdapter;
    private List<StkTagResBean> listBoss = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_tactic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivBoss1.setOnClickListener(this);
        mDataBinding.ivBoss2.setOnClickListener(this);
        mDataBinding.ivBoss3.setOnClickListener(this);
        mDataBinding.ivBoss4.setOnClickListener(this);
        mDataBinding.ivBoss5.setOnClickListener(this);
        mDataBinding.ivBoss6.setOnClickListener(this);
        mDataBinding.ivGuideMore.setOnClickListener(this);

        mDataBinding.rvGuideList.setLayoutManager(new LinearLayoutManager(mContext));
        guideTabAdapter = new GuideTabAdapter();
        mDataBinding.rvGuideList.setAdapter(guideTabAdapter);
        guideTabAdapter.addChildClickViewIds(R.id.tvGuideTabItemDetail);
        guideTabAdapter.setOnItemClickListener(this);
        guideTabAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivBoss1:
                goToDetail(0);
                break;
            case R.id.ivBoss2:
                goToDetail(1);
                break;
            case R.id.ivBoss3:
                goToDetail(2);
                break;
            case R.id.ivBoss4:
                goToDetail(3);
                break;
            case R.id.ivBoss5:
                goToDetail(4);
                break;
            case R.id.ivBoss6:
                goToDetail(5);
                break;
            case R.id.ivGuideMore://战术指南查看更多
                startActivity(GuideActivity.class);
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        GuideDetailActivity.stkResBean = guideTabAdapter.getItem(position);
        startActivity(GuideDetailActivity.class);
    }

    @Override
    protected void initData() {
        getBossData();
        getGuideData();
    }

    private void getBossData() {
        StkResApi.getChildTagAndResource(this, AppConfig.BOSS_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listBoss = result;
                    }
                }
            }
        });
    }

    private void getGuideData() {
        StkResApi.getTagResourceList(this, AppConfig.GUIDE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        guideTabAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void goToDetail(int position) {
        DetailActivity.stkResBean = listBoss.get(position).getDataList().get(0);
        startActivity(DetailActivity.class);
    }

}